//
//  DDDownLoadAddressACK.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDDownLoadAddressACK.h"
#import "DDCommentDic.h"
@implementation DDDownLoadAddressACK


- (id)initWithData:(NSData *)data {
    self = [super initWithData:data];
    return self;
}

- (id)initWithType:(int)type andOffset:(long)off andFilelength:(int)len andData:(NSData *)data{
    self = [super init];
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    NSData* typeData =[[NSData alloc]initWithBytes:&type length:INT_SIZE];
    [entity replaceBytesInRange:range withBytes:[typeData bytes]];
    
    range.location = range.location + range.length;
    range.length = LONG_SIZE;
    NSData* offData = [[NSData alloc]initWithBytes:&off length:LONG_SIZE];
    [entity replaceBytesInRange:range withBytes:[offData bytes]];
    
    range.location = range.location + range.length;
    range.length = INT_SIZE;
    NSData* lenData = [[NSData alloc]initWithBytes:&len length:INT_SIZE];
    [entity replaceBytesInRange:range withBytes:[lenData bytes]];
    
    range.location = range.location + range.length;
    range.length = [data length];
    [entity replaceBytesInRange:range withBytes:[data bytes]];
    [self setCmd:CloudCmd_DownloadAddressBookAck];
    [self setLength:[entity length]];
    int crc = [self crcs:entity withLength:[entity length]];
    [self setCrc:crc];
    return self;
}

- (int)getType{
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    
    NSData* typeData = [entity subdataWithRange:range];
    int type = [self dataToInt:typeData];
    return type;
}

-(long)getOffset{
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE;
    range.length = LONG_SIZE;
    
    NSData* offData = [entity subdataWithRange:range];
//    [self dataToLong:offData];
//    long offset = [self dataToLong:offData];
    long offset = 0;
    [offData getBytes:&offset length:LONG_SIZE];
    return offset;
}

-(int)getFileLength{
    NSRange range;
    range.location = HEADER_LENGTH + LONG_SIZE + INT_SIZE;
    range.length = INT_SIZE;
    
    NSData* lenData = [entity subdataWithRange:range];
    long len = [self dataToInt:lenData];
    return len;
}

-(NSData*)getData{
    NSRange range;
    range.location = HEADER_LENGTH + LONG_SIZE + INT_SIZE + INT_SIZE;
    range.length = [self length] - range.location;
    
    NSData* data = [entity subdataWithRange:range];
    return data;
}

-(void)dealloc{
    [self.entity release];
    [self.hdrs release];
    [super dealloc];
}
@end
